layui.config({
    base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage','laytpl','util'], function () {
    var form = layui.form
        , layer = parent.layer === undefined ? layui.layer : parent.layer
        , laypage = layui.laypage,laytpl = layui.laytpl, $ = layui.jquery;

    dataList(1);

    /**
     * 查询
     */
    form.on('submit(queryForm)', function(data){
        dataList(1);
        return false;
    });

    /**
     * 查询数据列表
     * @param number
     */
    function dataList(number) {
        var options = {
            dataType: "json",
            success: function (data) {
                if(data && data.error) {
                    layer.msg(data.error.msg);
                    return;
                }
                var listData = {"list":data.page.records};
                var getTpl = dataListTpl.innerHTML,view = document.getElementById('listDataBody');

                laytpl(getTpl).render(listData, function(html){
                    view.innerHTML = html;
                });
                form.render();
                if(number === 1) {
                    //分页标签
                    pageBar(data.page.total,data.page.size);
                }
            },
            error: function (e) {
                layer.msg(JSON.stringify(e));
            }
        };
        $("#listForm").ajaxSubmit(options);
    }

    /**
     * 数据分页
     * @param count
     * @param limit
     */
    function pageBar(count,limit) {
        laypage.render({
            elem: "page",
            limit: limit,
            count: count,
            theme: '#1E9FFF',
            layout: ['count', 'prev', 'page', 'next'],
            jump: function(obj, first){
                if(!first){
                    $("#number").val(obj.curr);
                    dataList(obj.curr);
                }
            }
        });
    }

    /**
     * 添加邮件模板类型
     */
    $("body").on("click", ".email_template_add_btn", function () {
        var index = layer.open({
            title: "创建邮件模板",
            type: 2,
            area: ['100%', '100%'],
            content: "/email/template/add",
            success: function (layero, index) {
                setTimeout(function () {
                    layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
    })

    /**
     * 编辑邮件模板类型
     */
    $("body").on("click", ".emailTemplateEdit", function () {
        var id = $(this).data("id");
        var index = layer.open({
            title: "查看博客",
            type: 2,
            area: ['100%', '100%'],
            content: "/email/template/"+id,
            success: function (layero, index) {
                setTimeout(function () {
                    layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
    })

    /**
     * 冻结邮件模板类型
     */
    form.on('switch(isShow)', function (obj) {
        var id = obj.elem.getAttribute("data-id");
        var status = 1;
        if (obj.elem.checked) {
            status = 0;
        }
        obj.elem.disabled=true;
        post("/email/template/frozen/" + id, {
            "status": status
        },function (data) {
            if (data.code == 0) {
                obj.elem.disabled=false;
                layer.msg(data.msg, {icon: 6,time:500});
            }
        });
    })
})
